#include "d3dbase.h"
#include "d3dutil.h"

namespace D3DUtil {

int rt_width, rt_height;

void SetRenderTarget(int target, LPDIRECT3DSURFACE9 surf) {
	D3DBase::GetDevice()->SetRenderTarget(target, surf);		
	D3DSURFACE_DESC desc;
	surf->GetDesc(&desc);
	rt_width = desc.Width;
	rt_height = desc.Height;
}

void SetRenderTarget(int target, LPDIRECT3DTEXTURE9 tex) {
	LPDIRECT3DSURFACE9 surf;
	tex->GetSurfaceLevel(target, &surf);
	SetRenderTarget(target, surf);
	surf->Release();
}

void Quad2d(float x1, float y1, float x2, float y2, unsigned int color) {
	float fx = (float)rt_width,
		  fy = (float)rt_height;
	struct UVVertex {
		float x,y,z,rhw;
		unsigned int color;
		float u,v;
	} coords[4] = {
		{x1*fx-0.5f,y1*fy-0.5f,0,1, color, 0.0f, 0.0f},
		{x2*fx-0.5f,y1*fy-0.5f,0,1, color, 1.0f, 0.0f},
		{x2*fx-0.5f,y2*fy-0.5f,0,1, color, 1.0f, 1.0f},
		{x1*fx-0.5f,y2*fy-0.5f,0,1, color, 0.0f, 1.0f},
	};
	D3DBase::GetDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1);
	D3DBase::GetDevice()->DrawPrimitiveUP(
		D3DPT_TRIANGLEFAN, 2, (void*)coords,sizeof(UVVertex));
}

}